﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace 玩具商城管理系统.znew
{
    public partial class 订购 : Form
    {
        public 订购()
        {
            InitializeComponent();
        }
        Class1 shopcarSrv = new Class1();
        public static int flag = 1;
        public static bool Aflag = false;//false为未登录，true为已登录
        public static string Commodityid = "";
        public int pagesize = 4;
        public int recoedCount = 0;
        public int pageCount = 0;
        public int currentPage = 1;
        SqlDataAdapter dalog;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from commodity";
            dalog = new SqlDataAdapter(str, DB.cn);
            dalog.Fill(ds, "log_info");
            DataTable dt = DB.GetDataSet(str);
           
        }
        void showAll()
        {
            
        }
        private void LoadPage()
        {
            recoedCount = ds.Tables["log_info"].Rows.Count;
            pageCount = recoedCount / pagesize;
            if ((recoedCount % pagesize) > 0)
            {
                pageCount++;
            }
            if (currentPage < 1) currentPage = 1;
            if (currentPage > pageCount) currentPage = pageCount;
            int beginRecord;
            int endRecord;
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            beginRecord = pagesize * (currentPage - 1);
            endRecord = pagesize * currentPage;

            if (currentPage == pageCount)
                endRecord = beginRecord;
            for (int i = beginRecord; i < endRecord; i++)
            {
                st.ImportRow(dt.Rows[i]);
            }
            dgvLog.DataSource = st;
            int a = 2 * (2 * currentPage - 1);
            int c = currentPage * 3 + currentPage - 1;
            int b = (currentPage - 1) * 4;
            int d = (currentPage - 1) * 3 + currentPage;
           
            if (d < dt.Rows.Count)
            {
                label7.Text = dt.Rows[d][1].ToString();
                label8.Text = dt.Rows[d][2].ToString();
                label7.Visible= true;
                label8.Visible= true;
                pictureBox2.Visible= true;
                button3.Visible= true;
            }
            else
            {
                label7.Text = "0";
                label8.Text = "0";
                label7.Visible = false;
                label8.Visible = false;
                pictureBox2.Visible = false;
                button3.Visible = false;
            }
            if (b < dt.Rows.Count)
            {
                label5.Text = dt.Rows[b][1].ToString();
                label6.Text = dt.Rows[b][2].ToString();
                label5.Visible= true;
                label6.Visible= true;
                pictureBox1.Visible= true;
                button4.Visible= true;
            }
            else
            {
                label5.Text = "0";
                label6.Text = "0";
                label5.Visible = false;
                label6.Visible = false;
                pictureBox1.Visible = false;
                button4.Visible= false;
            }
            if (a < dt.Rows.Count)
            {
                label14.Text = dt.Rows[a][1].ToString();
                label9.Text = dt.Rows[a][2].ToString();
                label14.Visible= true;
                label9.Visible= true;
                pictureBox3.Visible= true;
                button5.Visible= true;
            }
            else
            {
                label14.Text = "0";
                label9.Text = "0";
                label14.Visible = false;
                label9.Visible = false;
                pictureBox3.Visible = false;
                button5.Visible= false;
            }
            if (c < dt.Rows.Count)
            {
                label13.Text = dt.Rows[c][1].ToString();
                label15.Text = dt.Rows[c][2].ToString();
                label13.Visible= true;
                label15.Visible = true;
                pictureBox4.Visible= true;
                button6.Visible= true;
                label10.Visible = false;
            }
            else
            {
                label13.Text = "0";
                label15.Text = "0";
                label13.Visible = false;
                label15.Visible = false;
                pictureBox4.Visible = false;
                button6.Visible = false;
                label10.Visible = true;
            }
            try
            {
                pictureBox1.Image = Image.FromFile(Application.StartupPath + dt.Rows[(currentPage - 1) * 4][8].ToString());
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.gif.jpg");
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            try
            {
                pictureBox2.Image = Image.FromFile(Application.StartupPath + dt.Rows[(currentPage - 1) * 3 + currentPage][8].ToString());
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.gif.jpg");
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            try
            {
                pictureBox3.Image = Image.FromFile(Application.StartupPath + dt.Rows[currentPage * 3 + currentPage - 1][8].ToString());
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.gif.jpg");
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            try
            {
                pictureBox4.Image = Image.FromFile(Application.StartupPath + dt.Rows[2 * (2 * currentPage - 1)][8].ToString());
                pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.gif.jpg");
                pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }
        void show()
        {
           
        }
        private void 订购_Load(object sender, EventArgs e)
        {
            init();
            LoadPage();

        }

        private void label3_Click(object sender, EventArgs e)
        {
            currentPage--;
            LoadPage();
            int n = currentPage;
            
        }

        private void label4_Click(object sender, EventArgs e)
        {
            currentPage++;
            LoadPage();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            LoadPage();
            

        }

        private void dgvLog_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                login ll = new login();
                ll.ShowDialog();
            }
            else
            {
                DataTable dt = ds.Tables["log_info"];
                DataTable st = new DataTable();
                st = dt.Clone();
                if(textBox1.Text.ToString() == "")
                {
                    MessageBox.Show("亲请选择订购数量");
                }
                else if(int.Parse(textBox1.Text.ToString()) <=99){
                    MessageBox.Show("亲订购数量至少要100件");
                }
                else
                {
                    shopcarSrv.Add(login.username, Convert.ToInt32(dt.Rows[(currentPage - 1) * 4][0].ToString()), int.Parse(textBox1.Text));
                    Commodityid = dt.Rows[(currentPage - 1) * 4][0].ToString();
                    dg n1 = new dg();
                    n1.ShowDialog();

                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                login ll = new login();
                ll.ShowDialog();
            }
            else
            {
                DataTable dt = ds.Tables["log_info"];
                DataTable st = new DataTable();
                st = dt.Clone();
                if (textBox2.ToString() == "")
                {
                    MessageBox.Show("亲请选择订购数量");
                }
                else if (int.Parse(textBox2.Text) <= 99)
                {
                    MessageBox.Show("亲订购数量至少要100件");
                }
                else
                {
                    shopcarSrv.Add(login.username, Convert.ToInt32(dt.Rows[(currentPage - 1) * 3 + currentPage][0].ToString()), int.Parse(textBox2.Text));
                    Commodityid = dt.Rows[(currentPage - 1) * 3 + currentPage][0].ToString();

                    dg n2 = new dg();
                    n2.ShowDialog();
                }

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                login ll = new login();
                ll.ShowDialog();
            }
            else
            {
                DataTable dt = ds.Tables["log_info"];
                DataTable st = new DataTable();
                st = dt.Clone();
                if (textBox3.ToString() == "")
                {
                    MessageBox.Show("亲请选择订购数量");
                }
                else if (int.Parse(textBox3.Text) <= 99)
                {
                    MessageBox.Show("亲订购数量至少要100件");
                }
                else
                {
                    shopcarSrv.Add(login.username, Convert.ToInt32(dt.Rows[2 * (2 * currentPage - 1)][0].ToString()), int.Parse(textBox3.Text));
                    Commodityid = dt.Rows[2 * (2 * currentPage - 1)][0].ToString();
                    dg n3 = new dg();
                    n3.ShowDialog();
                }

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                login ll = new login();
                ll.ShowDialog();
            }
            else
            {
                DataTable dt = ds.Tables["log_info"];
                DataTable st = new DataTable();
                st = dt.Clone();
                if (textBox4.ToString() == "")
                {
                    MessageBox.Show("亲请选择订购数量");
                }
                else if (int.Parse(textBox4.Text) <= 99)
                {
                    MessageBox.Show("亲订购数量至少要100件");
                }
                else
                {
                    shopcarSrv.Add(login.username, Convert.ToInt32(dt.Rows[currentPage * 3 + currentPage - 1][0].ToString()), int.Parse(textBox4.Text));
                    Commodityid = dt.Rows[currentPage * 3 + currentPage - 1][0].ToString();
                    dg n4 = new dg();
                    n4.ShowDialog();
                }

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            Commodityid = dt.Rows[(currentPage - 1) * 4][0].ToString();
            Tcommodity tl = new Tcommodity();
            tl.ShowDialog();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            Commodityid = dt.Rows[(currentPage - 1) * 3 + currentPage][0].ToString();
            Tcommodity tl = new Tcommodity();
            tl.ShowDialog();
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            Commodityid = dt.Rows[2 * (2 * currentPage - 1)][0].ToString();
            Tcommodity tl = new Tcommodity();
            tl.ShowDialog();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            Commodityid = dt.Rows[currentPage * 3 + currentPage - 1][0].ToString();
            Tcommodity tl = new Tcommodity();
            tl.ShowDialog();
        }
    }
}
